package com.crape.faq.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.crape.common.exceptions.DAOException;
import com.crape.common.ibatis.SqlMapClientDaoSupportEx;
import com.crape.common.model.Question;
import com.crape.faq.dao.IQuestionDAO;

@Service
public class QuestionDAOImpl extends SqlMapClientDaoSupportEx implements IQuestionDAO {
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Question> searchQuestions(String searchValue, Integer startIndex, Integer pageSize) throws DAOException {
        if (startIndex == null || pageSize == null || pageSize == 0) {
            return null;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("searchValue", searchValue);
        params.put("startIndex", startIndex);
        params.put("pageSize", pageSize);
        return (List<Question>) this.queryList("searchQuestions", params);
    }

	@Override
	public Long countSearchQuestions(String searchValue) throws DAOException {
		Map<String, Object> params = new HashMap<String, Object>();
        params.put("searchValue", searchValue);
        return (Long) this.queryObject("countSearchQuestions", params);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> listQuestions(Question Question) throws DAOException {
        if (Question == null) {
            return null;
        }
        return (List<Question>) this.queryList("listQuestions", Question);
    }

    @Override
    public Long countQuestions(Question Question) throws DAOException {
        return (Long) this.queryObject("countQuestions", Question);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> getPagedQuestions(Integer startIndex, Integer pageSize) throws DAOException {
        if (startIndex == null || pageSize == null || pageSize == 0) {
            return null;
        }
        Map<String, Integer> params = new HashMap<String, Integer>();
        params.put("startIndex", startIndex);
        params.put("pageSize", pageSize);
        return (List<Question>) this.queryList("getPagedQuestions", params);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> getPagedQuestionsByAccountid(Long accountid, Integer startIndex, Integer pageSize)
                                                                                                                 throws DAOException {
        if (accountid == null || startIndex == null || pageSize == null || pageSize == 0) {
            return null;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountid", accountid);
        params.put("startIndex", startIndex);
        params.put("pageSize", pageSize);
        return (List<Question>) this.queryList("getPagedQuestionsByAccountid", params);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> getOther10Questions(Long questionid) throws DAOException {
        if (questionid == null) {
            return null;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("questionid", questionid);
        return (List<Question>) this.queryList("getOther10Questions", params);
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public List<Question> getHotQuestions() throws DAOException {
        return (List<Question>) this.queryList("getHotQuestions", null);
    }

    @Override
    public Long countAnswerQuestionsByAccountid(Long accountid) throws DAOException {
        if (accountid == null) {
            return null;
        }
        return (Long) this.queryObject("countAnswerQuestionsByAccountid", accountid);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> getPagedAnswerQuestionsByAccountid(Long accountid, Integer startIndex, Integer pageSize)
                                                                                                                    throws DAOException {
        if (accountid == null || startIndex == null || pageSize == null || pageSize == 0) {
            return null;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountid", accountid);
        params.put("startIndex", startIndex);
        params.put("pageSize", pageSize);
        return (List<Question>) this.queryList("getPagedAnswerQuestionsByAccountid", params);
    }
    
    @Override
	public Long countFollowedQuestionsByAccountid(Long accountid) throws DAOException {
        if (accountid == null) {
            return null;
        }
        return (Long) this.queryObject("countFollowedQuestionsByAccountid", accountid);
    }

	@SuppressWarnings("unchecked")
	@Override
	public List<Question> getPagedFollowedQuestionsByAccountid(Long accountid, Integer startIndex, Integer pageSize) throws DAOException {
        if (accountid == null || startIndex == null || pageSize == null || pageSize == 0) {
            return null;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountid", accountid);
        params.put("startIndex", startIndex);
        params.put("pageSize", pageSize);
        return (List<Question>) this.queryList("getPagedFollowedQuestionsByAccountid", params);
    }

    @Override
    public void insertQuestion(Question Question) throws DAOException {
        if (Question == null) {
            return;
        }
        this.insert("insertQuestion", Question);
    }

    @Override
    public void updateQuestion(Question Question) throws DAOException {
        if (Question == null) {
            return;
        }
        this.update("updateQuestion", Question);
    }

    @Override
    public void updateQuestionAnswers(Long id, Long num) throws DAOException {
        if (id == null || num == null) {
            return;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("num", num);
        this.update("updateQuestionAnswers", params);
    }

    @Override
    public void updateQuestionAttentions(Long id, Long num) throws DAOException {
        if (id == null || num == null) {
            return;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("num", num);
        this.update("updateQuestionAttentions", params);
    }

    @Override
    public void deleteQuestionById(Long id) throws DAOException {
        if (id == null) {
            return;
        }
        this.delete("deleteQuestionById", id);
    }

}
